package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.DataCleanResultMapper;
import cn.com.thinvent.dispatch.entity.DataCleanResult;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.DataCleanResultService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Component
public class DataCleanResultServiceImpl implements DataCleanResultService {
    @Resource
    private DataCleanResultMapper dataCleanResultMapper;
    @Override
    public PagedGridResult<DataCleanResult> list(Integer taskId, Integer pageNum, Integer pageSize) {
        PagedGridResult<DataCleanResult> pagedGridResult = new PagedGridResult<>();
        Integer pageBegin = (pageNum - 1) * pageSize;
        List<DataCleanResult> list = dataCleanResultMapper.queryAllByLimit(taskId, pageBegin, pageSize);
        Long count = dataCleanResultMapper.count(taskId);
        pagedGridResult.setRows(list);
        pagedGridResult.setRecords(count);
        pagedGridResult.setTotal((Integer.parseInt(count.toString()) + pageSize - 1 )/ pageSize);
        pagedGridResult.setPage(pageNum);
        return pagedGridResult;
    }

    @Override
    public DataCleanResult saveOrUpdate(DataCleanResult dataCleanResult) {
        Date date = new Date();
        if(dataCleanResult.getId() == null){
            dataCleanResult.setCreateTime(date);
            dataCleanResult.setUpdateTime(date);
            dataCleanResultMapper.insert(dataCleanResult);
        }else {
            dataCleanResult.setUpdateTime(date);
            dataCleanResultMapper.update(dataCleanResult);
        }

        return dataCleanResult;
    }

    @Override
    public DataCleanResult getById(Integer id) {
        DataCleanResult dataCleanResult = dataCleanResultMapper.queryById(id);
        return dataCleanResult;
    }
}
